//
//  MovieViewController.m
//  又是豆瓣
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "MovieViewController.h"
#import "mModel.h"
#import "MovieTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "NetTableViewCell.h"

@interface MovieViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *arrModel;

@property (nonatomic, retain) UIBarButtonItem *rightButton0;
@property (nonatomic, retain) UIBarButtonItem *rightButton1;

@end

@implementation MovieViewController

#define WI self.view.frame.size.width
#define HE self.view.frame.size.height

- (void)dealloc {
    [super dealloc];

}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WI, HE) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
//    _tableView.rowHeight = 150;
    _tableView.delegate = self;
    _tableView.dataSource = self;

    [_tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"cell1"];
    [_tableView registerClass:[NetTableViewCell class] forCellReuseIdentifier:@"cell2"];

    [_tableView release];
    
    NSString *path = [[NSBundle mainBundle ]pathForResource:@"movie" ofType:@"plist"];
    NSArray *arrData = [NSArray arrayWithContentsOfFile:path];
    
    self.arrModel = [NSMutableArray array];
    
    for (NSMutableDictionary *dic in arrData) {
        mModel *model = [mModel model];
        [model setValuesForKeysWithDictionary:dic];
        [_arrModel addObject:model];
    }
    
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    
  self.rightButton0 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(right0Action:)];
    self.navigationItem.rightBarButtonItem = _rightButton0;
    self.temp = 0;
    
    
    self.rightButton1 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_list@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(right0Action:)];

    
}

//点击方法
- (void)right0Action:(UIBarButtonItem *)barButtonItem{
    if (_temp == 0) {
             self.navigationItem.rightBarButtonItem = _rightButton1;
//        self.navigationItem.rightBarButtonItem = _rightButton0;
                [self.tableView reloadData];
        _temp = 1;
    }
    else if (_temp == 1){
             self.navigationItem.rightBarButtonItem = _rightButton0;
//        self.navigationItem.rightBarButtonItem = _rightButton1;
        [self.tableView reloadData];
     _temp = 0;
    }
    
}




//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_temp == 1) {
        
        if (_arrModel.count % 3 != 0) {
            return _arrModel.count / 3 + 1;
        }
        else return _arrModel.count / 3;
    } else if (_temp == 0) {
        
//        _temp = 1;
        return _arrModel.count;
    }
    
    else return 1;
//    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_temp == 0) {
        return 150;
    }
    else return 185;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_temp == 0) {
        MovieTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        
        cell1.imgV.image = [UIImage imageNamed:@"bg_eventlistcell.png"];
        
        mModel *mm = _arrModel[indexPath.row];
        [cell1.imageV sd_setImageWithURL:[NSURL URLWithString:mm.images[@"large"]]];
        cell1.title.text = mm.title;
        
        return cell1;
        
    }
        else if (_temp == 1){
        NetTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
            if (indexPath.row <= _arrModel.count / 3) {
            
        mModel *ma = _arrModel[indexPath.row *3 ];
        mModel *mb = _arrModel[indexPath.row *3 + 1];
        mModel *mc = _arrModel[indexPath.row *3 + 2];
                NSString *str1 = [NSString stringWithFormat:@"%@", ma.images[@"large"]];
                
            [cell2.imageV1 sd_setImageWithURL:[NSURL URLWithString:str1]];
            [cell2.imageV2 sd_setImageWithURL:[NSURL URLWithString:mb.images[@"large"]]];
            [cell2.imageV3 sd_setImageWithURL:[NSURL URLWithString:mc.images[@"large"]]];

            cell2.title1.text = ma.title;
            cell2.title2.text = mb.title;
                cell2.title3.text = mc.title;
            }
            
            else{
                mModel *ma = _arrModel[indexPath.row ];
                mModel *mb = _arrModel[indexPath.row + 1];
                [cell2.imageV1 sd_setImageWithURL:[NSURL URLWithString:ma.images[@"large"]]];
                [cell2.imageV2 sd_setImageWithURL:[NSURL URLWithString:mb.images[@"large"]]];

                cell2.title1.text = ma.title;
                cell2.title2.text = mb.title;

                
            }
            
            return cell2;
    }
        else return nil;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
